package com.qian.login;

import com.qian.Test.TestDemo;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Login {
    public static void homepage(){
        System.out.println("==========欢迎来到学生管理系统：========");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.忘记密码");
        System.out.println("请输入操作：");
    }

    //注册功能
    public static void userRegister(ArrayList<User> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = sc.next();
        //1.用户名长度必须在3-15之间
        if(!(name.length() >= 3 && name.length() <= 15)){
            System.out.println("用户名长度必须在3-15之间");
            return;
        }


        //2.只能是字母加数字的组合，但是不能是纯数字
        //判断用户名
        //字符串转换为字符数组
        char[] strName = name.toCharArray();

        int count1 = 0;
        int count2 = 0;
        int count3 = 0;

        for (int i = 0; i < strName.length; i++) {
            if(strName[i] >= 48 && strName[i] <= 57){
                count1++;
            }else if(strName[i] >= 65 && strName[i] <= 90) {
                count2++;
            }else if(strName[i] >= 97 && strName[i] <= 122){
                count3++;
            }else{
                System.out.println("用户名只能是字母加数字的组合");
                break;
            }
        }

        if(!(count1 >= 1 && (count2 >= 1 || count3 >= 1))){
            System.out.println("用户名只能是字母加数字的组合");
            return;
        }

        //3.用户名唯一
        boolean flga = verifyName(list,name);
        if(flga) {
            System.out.println("用户名已经被注册！");
            return;
        }


        //经过重重判断，开始注册用户

        System.out.println("请输入密码");
        String pass = sc.next();
        System.out.println("请再次输入密码：");
        String pass2 = sc.next();

        if(!(pass.equals(pass2))){
            System.out.println("两次密码不一致！");
            return;
        }
        System.out.println("密码一致！");

        System.out.println("请输入身份证号：");
        String idnumber = sc.next();
        //1.长度为18位
        if (!(idnumber.length() == 18)){
            System.out.println("身份证必须是18位");
            return;
        }
        //2.不能以0为开头
        char[] idarr = idnumber.toCharArray();
        if(idarr[0] == 0){
            System.out.println("身份证不能以0为开头");
            return;
        }
        //3.前17位，必须都是数字
        for (int i = 0; i < idarr.length-1; i++) {
            if(!(idarr[i] >= 48 && idarr[i] <= 57)){
                System.out.println("前17位，必须都是数字");
                return;
            }
        }

        //手机号验证
        System.out.println("输入手机号：");
        String pnumber = sc.next();
        //15519881343
        String regex = "[1]\\d{10}";
        Matcher matcher = Pattern.compile(regex).matcher(pnumber);
        boolean bool = matcher.matches();

        if(!bool){
            System.out.println("电话号码格式不对！");
            return;
        }

        //全部验证成功写入对象
        User userin = new User(name,pass,idnumber,pnumber);
        list.add(userin); //对象加入集合
    }
    //判断姓名是否相同
    public static boolean verifyName(ArrayList<User> user,String name){
        for (int i = 0; i < user.size(); i++) {
            if(user.get(i).getUsername().equals(name)) {
                return true;
            }
        }
        return false;
    }

    public static boolean verifyPass(ArrayList<User> user,String pass){
        for (int i = 0; i < user.size(); i++) {
            if(user.get(i).getPassword().equals(pass)) {
                return true;
            }
        }
        return false;
    }



    //登录功能
    public static void login(ArrayList<User> list){
        Scanner sc = new Scanner(System.in);
        String username;
        String pass;
        // 1.键盘录入用户名
        int count = 0;

        System.out.println("输入用户名：");
        username = sc.next();
        boolean bool = verifyName(list,username);
        if(!bool){
            System.out.println("用户名不存在，请先注册！");
            return;
        }


        while(true) {
            //2.键盘录入密码
            System.out.println("输入密码：");
            pass = sc.next();
            boolean bool2 = verifyPass(list,pass);
            if(!bool2){
                count++;
                System.out.println("密码错误！,重新输入");
                if(count == 3){
                    System.out.println("多次密码输入错误！暂停密码功能");
                    return;
                }
                continue;
            }


            break;
        }

        //3.键盘录入验证码
        String ver = verificationCode();
        System.out.println("验证码为："+ver);
        System.out.println("输入验证码：");
        while (true){
            String code = sc.next();
            if(!(ver.equalsIgnoreCase(code))){
                System.out.println("验证码错误,请重新输入：");
                continue;
            }
            break;
        }


        System.out.println("登录成功！");
        //跳转学生管理系统
        TestDemo.test01();
    }


    //生成验证码方法
    public static String verificationCode(){
        //返回生成好的验证码
        //随机数
        Random r = new Random();
        String str = "";
        for (int j = 0; j < 5; j++) {
            int i = r.nextInt(3);

            switch (i){
                case 0:
                    //随机生成26个字母
                    char ch1 = (char)(r.nextInt(26)+97);
                    str+= ch1;
                    break;
                case 1:
                    char ch2 = (char)(r.nextInt(26)+65);
                    str+= ch2;
                    break;
                default:
                    int ch3 = r.nextInt(10);
                    str+= ch3;
                    break;
            }
        }

        return str;
    }





    //遗忘密码
    public static void forgetThePassword(ArrayList<User> list){
        System.out.println("输入用户名：");
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        boolean bool = verifyName(list,username);
        if(!bool){
            System.out.println("用户名不存在请前往注册！");
            return;
        }

        System.out.println("请输入身份证号码：");
        String idNumber = sc.next();
        System.out.println("输入手机号码");
        String pNumber = sc.next();

        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(username)){
                if(list.get(i).getIdnumber().equals(idNumber) && list.get(i).getPhonenumber().equals(pNumber)){

                    System.out.println("请输入新密码：");
                    String pass = sc.next();
                    list.get(i).setPassword(pass);
                    break;
                }
                System.out.println("身份证和手机号不匹配！");
                break;
            }
        }








    }







}
